package com.user.query.test;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 自动登录的一些思路
 */
public class AutoLogin {
    /**
     *
     * //6.自动登录
     if(autologin){
     BasUsertokenProxy usertokenProxy=new BasUsertokenProxy();
     //添加授权信息  在filter里面可以进行自动登录判断 获取凭证，更新凭证和cookie
     BasUsertoken userToken=usertokenProxy.add(user.getUserId(), DateUtils.getNowDString(Constants.Sercurity.autologinExpire));
     Cookie cookieAutologin=new Cookie(Constants.Key.autologin,userToken.getUsertokenVal());
     try {
     cookieAutologin.setPath(MMVCServlet.contextPath);
     cookieAutologin.setMaxAge(NumberUtils.toInt(Constants.Sercurity.autologinExpire/1000));
     } catch (ConvertException e) {
     e.printStackTrace();
     }
     response.addCookie(cookieAutologin);
     }
     */


    /**
     * 获取Cookie
     * @param request
     * @param cookieName
     * @return
     */
    public static Cookie getCookie(HttpServletRequest request, String cookieName){
        if(request==null) return null;
        Cookie [] cookies=request.getCookies();
        if(cookies==null) return null;
        for (int i = 0; i < cookies.length; i++) {
            if(cookies[i].getName().equals(cookieName)) return cookies[i];
        }
        return null;
    }
}
